/*
 * Copyright (c) 2017, Li Bo/Alex Lee All rights reserved.
 * 
 * http://www.libo4dream.club/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.bmsp.tool;

import java.lang.reflect.Field;

import com.google.common.base.CaseFormat;
import com.jfinal.plugin.activerecord.Model;

/**
 * @ClassName: ClassRuleUtil
 * @Description: 类规则操作工具类
 * @author: LiBo/Alex Lee
 * @date: 2017年9月20日 上午12:01:10
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public class ClassRuleUtil {

	
		/**
		 * @Title: transfer
		 * @Description: 针对于驼峰标识进行转换数据库中和java驼峰标志的字段
		 * @param v 数据库字段
		 * @param t java类字段
		 * @return
		 * @return: V
		 * @throws IllegalAccessException 
		 * @throws IllegalArgumentException 
		 */
		public static <V,T> T db2JavaClass(Model v,T t) throws IllegalArgumentException, IllegalAccessException{
			//Class clazz  = v.getClass();
			//Field[] fields = clazz.getDeclaredFields();
			String [] fields = v._getAttrNames();
			for(String field:fields){
				Field[] fields2 = t.getClass().getDeclaredFields();
				for(Field field2:fields2){
					String fieldName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, field);
					if(fieldName.equals(field2.getName()))
					{
						field2.setAccessible(true);
						switch(field2.getType().toString()){
						case "class java.lang.String":
							 field2.set(t,v.get(field));
							 break;
						case "class java.lang.Integer":
							 field2.set(t,Integer.parseInt(v.get(field)+""));
							 break;
						case "class java.lang.Boolean":
							 field2.set(t,Boolean.valueOf(v.get(field)+""));
							 break;
						case "class java.lang.Long":
							 field2.set(t,Long.parseLong(v.get(field)+""));
							 break;
						}
					}
				}
			}
			return t;
		}
		
		
		/**
		 * @Title: javaClass2Db
		 * @Description: TODO
		 * @param v
		 * @param t
		 * @return
		 * @throws IllegalArgumentException
		 * @throws IllegalAccessException
		 * @return: T
		 */
		public static <V,T> T javaClass2Db(V v,T t) throws IllegalArgumentException, IllegalAccessException{
			Class clazz  = v.getClass();
			Field[] fields = clazz.getDeclaredFields();
			for(Field field:fields){
				Field[] fields2 = t.getClass().getDeclaredFields();
				for(Field field2:fields2){
					String fieldName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName());
					if(fieldName.equals(field2.getName()))
					{
						switch(field2.getType().toString()){
						case "class java.lang.String":
							 field2.set(t,field.get(v));
							 break;
						case "class java.lang.Integer":
							 field2.set(t,Integer.parseInt(field.get(v)+""));
							 break;
						case "class java.lang.Boolean":
							 field2.set(t,Boolean.valueOf(field.get(v)+""));
							 break;
						case "class java.lang.Long":
							 field2.set(t,Long.parseLong(field.get(v)+""));
							 break;
						}
					}
				}
			}
			return t;
		}
	
}
